import request from '@/utils/request';
import type { ApiResult, PageResult } from '@/api';
import type { Article, ArticleParam } from './model';

/**
 * 分页查询
 */
export async function pageArticles(params: ArticleParam) {
  const res = await request.get<ApiResult<PageResult<Article>>>(
    '/content/article/page',
    { params }
  );
  if (res.data.code === 0) {
    return res.data.data;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 添加
 */
export async function addArticle(data: Article) {
  const res = await request.post<ApiResult<unknown>>('/content/article', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改
 */
export async function updateArticle(data: Article) {
  const res = await request.put<ApiResult<unknown>>('/content/article', data);
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 删除
 */
export async function removeArticle(id?: number) {
  const res = await request.delete<ApiResult<unknown>>(
    '/content/article/' + id
  );
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}

/**
 * 修改状态
 */
export async function updateArticleStatus(id?: number, status?: number) {
  const res = await request.put<ApiResult<unknown>>('/content/article/status', {
    id,
    status
  });
  if (res.data.code === 0) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
}
